# 网关API测试脚本
Write-Host "========================================" -ForegroundColor Green
Write-Host "网关API测试" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""

# 测试函数
function Test-GatewayAPI {
    param(
        [string]$Url,
        [string]$Description
    )
    
    Write-Host "测试: $Description" -ForegroundColor Yellow
    Write-Host "URL: $Url" -ForegroundColor Gray
    
    try {
        $response = Invoke-RestMethod -Uri $Url -Method GET -TimeoutSec 10
        Write-Host "[成功]" -ForegroundColor Green
        
        # 显示关键信息
        if ($response.code -eq 200) {
            Write-Host "响应码: $($response.code)" -ForegroundColor Cyan
            Write-Host "消息: $($response.message)" -ForegroundColor Cyan
            
            if ($response.data.products) {
                Write-Host "商品数量: $($response.data.products.Count)" -ForegroundColor Cyan
                if ($response.data.products.Count -gt 0) {
                    $firstProduct = $response.data.products[0]
                    Write-Host "第一个商品: $($firstProduct.title) - ¥$($firstProduct.price)" -ForegroundColor Cyan
                }
            }
        } else {
            Write-Host "响应: $($response | ConvertTo-Json -Depth 2)" -ForegroundColor Cyan
        }
        
        return $true
    }
    catch {
        Write-Host "[失败] $($_.Exception.Message)" -ForegroundColor Red
        return $false
    }
    finally {
        Write-Host ""
    }
}

# 1. 检查网关健康状态
Write-Host "1. 检查网关服务状态..." -ForegroundColor Blue
try {
    $gatewayHealth = Invoke-RestMethod -Uri "http://localhost:5200/health" -TimeoutSec 5
    Write-Host "[成功] 网关服务正在运行" -ForegroundColor Green
    Write-Host "健康状态: $gatewayHealth" -ForegroundColor Cyan
}
catch {
    Write-Host "[错误] 网关服务未运行，请先启动网关服务" -ForegroundColor Red
    Write-Host "启动命令: dotnet run --project hao-cloud-api\hao-Gateway" -ForegroundColor Yellow
    Read-Host "Press Enter to exit"
    exit 1
}
Write-Host ""

# 2. 测试通过网关访问商品服务
Write-Host "2. 测试网关路由 - 商品服务..." -ForegroundColor Blue
$productResult = Test-GatewayAPI -Url "http://localhost:5200/api/product/list?page=1&pageSize=3" -Description "获取商品列表"

# 3. 测试通过网关访问用户服务
Write-Host "3. 测试网关路由 - 用户服务..." -ForegroundColor Blue
$userResult = Test-GatewayAPI -Url "http://localhost:5200/api/user/test" -Description "用户服务测试"

# 4. 测试通过网关访问订单服务
Write-Host "4. 测试网关路由 - 订单服务..." -ForegroundColor Blue
$orderResult = Test-GatewayAPI -Url "http://localhost:5200/api/order/test/services" -Description "订单服务发现测试"

# 5. 测试结果汇总
Write-Host "========================================" -ForegroundColor Green
Write-Host "测试结果汇总" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green

$results = @(
    @{Name="商品服务路由"; Result=$productResult},
    @{Name="用户服务路由"; Result=$userResult},
    @{Name="订单服务路由"; Result=$orderResult}
)

$passedTests = 0
$totalTests = $results.Count

foreach ($result in $results) {
    $status = if ($result.Result) { "✅ 通过" } else { "❌ 失败" }
    $color = if ($result.Result) { "Green" } else { "Red" }
    
    Write-Host "$($result.Name): $status" -ForegroundColor $color
    
    if ($result.Result) {
        $passedTests++
    }
}

Write-Host ""
Write-Host "总计: $passedTests/$totalTests 路由测试通过" -ForegroundColor $(if ($passedTests -ge 1) { "Green" } else { "Red" })

if ($passedTests -ge 1) {
    Write-Host ""
    Write-Host "🎉 网关基本功能正常！" -ForegroundColor Green
    Write-Host "移动端现在可以通过网关访问微服务" -ForegroundColor Green
    Write-Host "网关地址: http://localhost:5200" -ForegroundColor Cyan
} else {
    Write-Host ""
    Write-Host "⚠️  网关路由测试失败，请检查：" -ForegroundColor Yellow
    Write-Host "- 确保网关服务正在运行 (端口5200)" -ForegroundColor Yellow
    Write-Host "- 确保各微服务正在运行" -ForegroundColor Yellow
    Write-Host "- 检查ocelot.json路由配置" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "📋 网关路由配置：" -ForegroundColor Cyan
Write-Host "- /api/user/* → 用户服务 (5006)" -ForegroundColor Gray
Write-Host "- /api/product/* → 商品服务 (5233)" -ForegroundColor Gray
Write-Host "- /api/order/* → 订单服务 (5234)" -ForegroundColor Gray

Write-Host ""
Read-Host "Press Enter to exit"